package com.wex.cookbook.exception;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * ClassName: BizAssertUtils
 * description: 自定义断言类
 * @author wsy
 * @Date: 2022/11/22 20:15
 * @since JDK 1.8
 */
public class BizAssertUtils {
    private static Logger logger = LoggerFactory.getLogger(BizAssertUtils.class);

    public BizAssertUtils() {
    }

    public static void isNull(Object object, String message, Object... params) {
        if (object != null) {
            logger.warn("应该为空: object={}, message={}, params={}", new Object[]{object, message, params});
            throw new BusinessException(message);
        }
    }
 
    public static void notNull(Object object, String message, Object... params) {
        if (object == null) {
            logger.warn("不应为空: object={}, message={}, params={}", new Object[]{null, message, params});
            throw new BusinessException(message);
        }
    }

 

    public static void isEmpty(Collection<?> collection, String message, Object... params) {
        if (collection != null && collection.size() != 0) {
            logger.warn("应该为空: collection={}, message={}, params={}", new Object[]{collection, message, params});
            throw new BusinessException(message);
        }
    }
 

    public static void notEmpty(Collection<?> collection, String message, Object... params) {
        if (collection == null || collection.size() == 0) {
            logger.warn("不应为空: collection={}, message={}, params={}", new Object[]{collection, message, params});
            throw new BusinessException(message);
        }
    }
//    public static void Empty(Collection<?> collection, String message, Object... params) {
//        if (collection != null || collection.size() > 0) {
//            logger.warn("应为空: collection={}, message={}, params={}", new Object[]{collection, message, params});
//            throw new BusinessException(message);
//        }
//    }




    public static void notEmpty(Map<?, ?> map, String message, Object... params) {
        if (map == null || map.size() == 0) {
            logger.warn("不应为空: map={}, message={}, params={}", new Object[]{map, message, params});
            throw new BusinessException(message);
        }
    }

 

    public static void isBlank(String string, String message, Object... params) {
        if (StringUtils.isNotBlank(string)) {
            logger.warn("应该为空: string={}, message={}, params={}", new Object[]{string, message, params});
            throw new BusinessException(message);
        }
    }

 

    public static void notBlank(String string, String message, Object... params) {
        if (!StringUtils.isNotBlank(string)) {
            logger.warn("不应为空: string={}, message={}, params={}", new Object[]{string, message, params});
            throw new BusinessException(message);
        }
    }

 

    public static void isTrue(boolean flag, String message, Object... params) {
        if (!flag) {
            logger.warn("应该为TRUE: flag={}, message={},  params={}", new Object[]{flag, message, params});
            throw new BusinessException(message);
        }
    }
 

    public static void isFalse(boolean flag, String message, Object... params) {
        if (flag) {
            logger.warn("应该为FALSE: flag={}, message={}, params={}", new Object[]{flag, message, params});
            throw new BusinessException(message);
        }
    }

 

    public static <T extends Enum<T>> void legalEnum(Enum<T> actual, Enum<T> expected, String message, Object... params) {
        if (actual != expected) {
            logger.warn("状态错误：message={}, actual={}, expected={}, params={}", new Object[]{message, actual, expected, params});
            throw new BusinessException(message);
        }
    }
 

    public static <T extends Enum<T>> void legalEnums(String message, Enum<T> actual, Enum<T>... expected) {
        List<Enum<T>> enumList = Arrays.asList(expected);
        if (!enumList.contains(actual)) {
            logger.warn("状态错误：message={}, actual={}, expected={}", new Object[]{message, actual, expected});
            throw new BusinessException(message);
        }
    }
 

    public static <T extends Enum<T>> void legalEnums(Enum<T> actual, Enum<T>[] expected, String message, Object... params) {
        List<Enum<T>> enumList = Arrays.asList(expected);
        if (!enumList.contains(actual)) {
            logger.warn("状态错误：message={}, actual={}, expected={}, params={}", new Object[]{message, actual, expected, params});
            throw new BusinessException(message);
        }
    }
 

    public static void isEquals(String actual, String expected, String message, Object... params) {
        if (!Objects.equals(actual, expected)) {
            logger.warn("应该为相同：message={}, actual={}, expected={}, params={}", new Object[]{message, actual, expected, params});
            throw new BusinessException(message);
        }
    }
 

    public static void notEquals(String actual, String expected, String message, Object... params) {
        if (Objects.equals(actual, expected)) {
            logger.warn("应该不相同：message={}, actual={}, expected={}, params={}", new Object[]{message, actual, expected, params});
            throw new BusinessException(message);
        }
    }
 

    public static void isEquals(Long actual, Long expected, String message, Object... params) {
        if (!Objects.equals(actual, expected)) {
            logger.warn("应该为相同：message={}, actual={}, expected={}, params={}", new Object[]{message, actual, expected, params});
            throw new BusinessException(message);
        }
    }
 

    public static void notEquals(Long actual, Long expected, String message, Object... params) {
        if (Objects.equals(actual, expected)) {
            logger.warn("应该不相同：message={}, actual={}, expected={}, params={}", new Object[]{message, actual, expected, params});
            throw new BusinessException(message);
        }
    }
 

    public static void fail(String message, Object... params) {
        logger.warn("应该为失败: message={}, params={}", message, params);
        throw new BusinessException(message);
    }
 

    public static void isContains(String actual, Collection<String> expected, String message, Object... params) {
        if (!expected.contains(actual)) {
            logger.warn("匹配错误: actual={},expected={}, message={},params={}", new Object[]{actual, expected, message, params});
            throw new BusinessException(message);
        }
    }
 

    public static void notContains(String actual, Collection<String> expected, String message, Object... params) {
        if (expected.contains(actual)) {
            logger.warn("应该为不包含: actual={},expected={}, message={},params={}", new Object[]{actual, expected, message, params});
            throw new BusinessException(message);
        }
    }

    public static void Return(Integer code, String message) {
        throw new BusinessException(code,message);
    }
}
